<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 特殊属性
 * Class Category
 * @author  heyu
 */
class Attribute extends MY_Controller {
    /**
     * @var ConsumerFindAttributeModel
     */
    public $mAttribute;
    /**
     * @var ConsumerFindCategoryModel
     */
    public $mFindCategory;

	public function __construct(){
		parent::__construct();
        $this->loadModel('ConsumerFindAttributeModel', 'mAttribute', 'consumer');
        $this->loadModel('ConsumerFindCategoryModel', 'mFindCategory', 'consumer');
	}

	public function index(){
        $data = array(
            'title' => '特殊属性分类'
        );
        $this->view('find/attribute/category', $data, 'datatable,validation,datepicker,select2,nestable');
    }

    /**
     * 获取数据
     */
    public function getCategoryList(){
        $data = $this->mAttribute->adminPcGetCategoryList();
        echo json_encode($data);
    }

    /**
     * 获取分类详情
     */
    public function getCategoryInfo(){
        $id = (int)post('id');
        if (!$id){
            $this->ajaxReturn(0, '数据错误');
        }
        $info = $this->mAttribute->getCategoryInfo($id);
        if (!$info){
            $this->ajaxReturn(0, '分类不存在');
        }
        $this->ajaxReturn(1, '', $info);
    }

    /**
     * 保存
     */
    public function saveCategory(){
        //表单验证
        $vConf = array(
            array('name', '分类名称', 'required'),
            array('sort', '排序', 'is_natural_no_zero'),
        );
        $this->formDataValidation($vConf);
        $sort = (int)post('sort');
        $id = (int)post('id');

        $data = array('name' => post('name'));
        if ($id){
            $info = $this->mAttribute->getCategoryInfo($id);
            if (!$info){
                $this->ajaxReturn(0, '分类不存在');
            }
            if ($sort){
                $data['sort'] = $sort;
            }
        }else{
            $data['sort'] = $sort ? $sort : $this->mAttribute->getMaxSort();
            $data['addtime'] = time();
        }
        $tA = $this->mAttribute->saveCategory($data, $id);
        if (!$tA){
            $this->ajaxReturn(0, '保存失败');
        }
        $this->ajaxReturn(1, '保存成功');
    }

    /**
     * 分类排序
     */
    public function modelSort(){
        $data = post('data');
        if (empty($data)){
            $this->ajaxReturn(0, '数据错误');
        }
        if(is_array($data)) {
            $count = count($data);
            foreach ($data as $k => $v) {
                $dbData[] = array('id' => $v['id'], 'sort' => $count);
                $count--;
            }
            if ($dbData) {
                $this->mAttribute->saveBatchCategory($dbData, 'id');
            }
        }
        $this->ajaxReturn(1, '保存成功');
    }

    /**
     * 特殊属性分类删除
     */
    public function delete(){
        //表单验证
        $vConf = array(
            array('id', 'id', 'required|is_natural_no_zero'),
        );
        $this->formDataValidation($vConf, array('id' => get('id')));
        $id = (int)get('id');
        $info = $this->mAttribute->getCategoryInfo($id);
        if (!$info){
            $this->ajaxReturn(0, '数据不存在或已删除');
        }
        $attribute = $this->mAttribute->getList($id);
        if ($attribute){
            $this->ajaxReturn(0, '请先删除分类下的特殊属性');
        }
        $tA = $this->mAttribute->deleteAttributeCategory($id);
        if (!$tA){
            $this->ajaxReturn(0, '删除失败');
        }
        $this->ajaxReturn(1, '删除成功');
    }

/******************属性值***********************/

	/**
	 * 首页
	 */
	public function attribute(){
	    $id = (int)get('id');
	    if (!$id){
	        show_404();
        }
        $info = $this->mAttribute->getCategoryInfo($id);
	    if (!$info){
	        show_404();
        }

        $data = array(
            'type' => $this->mAttribute->getType(),
//            'category' => $this->mFindCategory->getList(),
            'info' => $info,
            'title' => '属性值'
        );
		$this->view('find/attribute/home', $data, 'datatable,validation,datepicker,select2');
	}

    /**
     * 获取数据
     */
    public function getList(){
        $id = (int)get('id');
        $data = $this->mAttribute->adminPcGetList($id);
        foreach ($data['data'] as $k => $v){
            $data['aaData'][$k][] = $v['attr_name'];
            $data['aaData'][$k][] = $this->mAttribute->getType($v['attr_input_type']);
            if ($v['attr_input_type'] == 3){
                $attr_values = json_decode($v['attr_values'], true);
                $data['aaData'][$k][] = implode('|', $attr_values);
            }else{
                $data['aaData'][$k][] = '';
            }
            $data['aaData'][$k][] = dateFormat($v['addtime']);

            $btnConfig = adminCreateBtn(array(
                'label' => '修改',
                'attr' => array(
                    'href' => 'javascript:;',
                    'class' => 'btn btn-xs blue-stripe default edit-btn',
                    'data-id' => $v['id']
                ),
                'tag' => 'a',
                'permissions' => array(
                    'module' => 'group',
                    'key' => 'status'
                )
            ));
            $btnConfig .=' '. adminCreateBtn(array(
                'label' => '删除',
                'attr' => array(
                    'href' => getUrl('find/attribute/deleteAttribute?id='.$v['id']),
                    'class' => 'ajax-request-btn btn btn-xs red-stripe default',
                    'alert-message' => '是否确认删除 “ '.$v['attr_name'].' ” ?',
                ),
                'tag' => 'a',
                'permissions' => array(
                    'module' => 'group',
                    'key' => 'status'
                )
            ));

            $data['aaData'][$k][] = $btnConfig;
        }
        unset($data['data']);
        echo json_encode($data);
    }

    /**
     * 获取分类详情
     */
    public function getInfo(){
        $id = (int)post('id');
        if (!$id){
            $this->ajaxReturn(0, '数据错误');
        }
        $info = $this->mAttribute->getInfo($id);
        if (!$info){
            $this->ajaxReturn(0, '分类不存在');
        }
        $this->ajaxReturn(1, '', $info);
    }

    /**
     * 保存
     */
    public function save(){
        //表单验证
        $vConf = array(
            array('attribute_category_id', '分类id', 'required|is_natural_no_zero'),
            array('attr_name', '名称', 'required'),
            array('attr_input_type', '类型', 'required|in_list[1,2,3,4,5]'),
        );
        $attr_input_type = (int)post('attr_input_type');
        if ($attr_input_type == 3){
            $vConf[] = array('attr_values[]', '选择输入值', 'required');
        }
        $this->formDataValidation($vConf);
        $id = (int)post('id');
        $attributeCategoryId = (int)post('attribute_category_id');
        $attr_values = post('attr_values');

        // 验证分类
        $category = $this->mAttribute->getCategoryInfo($attributeCategoryId);
        if (!$category){
            $this->ajaxReturn(0, '分类不存在');
        }
        if (!empty($attr_values)){
            $attr_values = json_encode($attr_values, JSON_UNESCAPED_UNICODE);
        }else{
            $attr_values = '';
        }
        $data = array(
            'attr_name' => trim(post('attr_name')),
            'attribute_category_id' => $attributeCategoryId,
            'attr_input_type' => $attr_input_type,
            'attr_values' => $attr_values,
        );
        if ($id){
            $info = $this->mAttribute->getInfo($id);
            if (!$info){
                $this->ajaxReturn(0, '数据不存在');
            }
        }else{
            $data['addtime'] = time();
        }
        $tA = $this->mAttribute->save($data, $id);
        if (!$tA){
            $this->ajaxReturn(0, '保存失败');
        }
        $this->ajaxReturn(1, '保存成功');
    }

    /**
     * 删除
     */
    public function deleteAttribute(){
        //表单验证
        $vConf = array(
            array('id', 'id', 'required|is_natural_no_zero'),
        );
        $this->formDataValidation($vConf, array('id' => get('id')));
        $id = (int)get('id');
        $info = $this->mAttribute->getInfo($id);
        if (!$info){
            $this->ajaxReturn(0, '数据不存在或已删除');
        }
        $tA = $this->mAttribute->deleteAttribute($id);
        if (!$tA){
            $this->ajaxReturn(0, '删除失败');
        }
        $this->ajaxReturn(1, '删除成功');
    }

}
